package com.cskaoyan.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author stone
 * @date 2021/11/18 16:32
 */
public class LifeCycleBean implements BeanNameAware, BeanFactoryAware,
        ApplicationContextAware, InitializingBean, DisposableBean {
    String parameter;

    String beanName;
    BeanFactory beanFactory;
    ApplicationContext applicationContext;

    public LifeCycleBean() {
        System.out.println("1、实例化");
    }

    public void setParameter(String parameter) {
        System.out.println("2、设置参数：" + parameter);
        this.parameter = parameter;
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("3、BeanNameAware的setBeanName方法，传入的参数为：" + name);
        this.beanName = name;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("4、BeanFactoryAware的setBeanFactory方法");
        this.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("5、ApplicationContextAware的setApplicationContext方法");
        this.applicationContext = applicationContext;
    }

    //自定义init方法 👉 方法名任意写
    public void init() {
        System.out.println("自定义init");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean的afterPropertiesSet方法");
    }

    public void customDestroy() {
        System.out.println("自定义的destroy");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean提供的destroy");
    }
    @Override
    public String toString() {
        return "LifeCycleBean{" +
                "parameter='" + parameter + '\'' +
                '}';
    }

}
